package com.example.h1nson.realphotography.activity;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.h1nson.realphotography.R;
import com.example.h1nson.realphotography.activity.imageloader.ImageGridActivity;
import com.example.h1nson.realphotography.asyncTask.UserInfoAsynTask;
import com.example.h1nson.realphotography.config.ConfigURL;
import com.example.h1nson.realphotography.config.GetCurrentUser;
import com.example.h1nson.realphotography.interface_photo.OnDataFinishedListener;
import com.example.h1nson.realphotography.util.AsyncImageLoader;

import org.json.JSONException;
import org.json.JSONObject;

public class UserInfoActivity extends AppCompatActivity {
    private ImageView iamgeView_face;
    private TextView textView_name,textView_email,textView_des,
            textView_address,textView_id,textView_born,textView_sex;
    private ImageButton imageButton_pic;
    private Button btn_focus;
    private int userId;
    private boolean focus;
    private String user_name,user_email,user_des,user_address,user_born,user_sex,user_face_URL,user_firstPic_URL;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_info);
        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        userId = bundle.getInt("userId");
        iamgeView_face = (ImageView)findViewById(R.id.user_info_userface);
        textView_name = (TextView)findViewById(R.id.user_info_name);
        textView_email = (TextView)findViewById(R.id.user_info_email);
        textView_des = (TextView)findViewById(R.id.user_info_des);
        textView_address = (TextView)findViewById(R.id.user_info_address);
        textView_id = (TextView)findViewById(R.id.user_info_id);
        textView_born = (TextView)findViewById(R.id.user_info_born);
        textView_sex = (TextView)findViewById(R.id.user_info_sex);
        imageButton_pic = (ImageButton)findViewById(R.id.user_info_btn_pic);
        btn_focus = (Button)findViewById(R.id.user_info_btn_focus);
        init();
        btnListener();
    }
    private void btnListener(){
        imageButton_pic.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(UserInfoActivity.this, ImageGridActivity.class);
                intent.putExtra("serverURL", "userInfo");
                intent.putExtra("userId",userId);
                intent.putExtra("userName",user_name);
                startActivity(intent);
            }
        });
        btn_focus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                btn_focus.setEnabled(false);
                JSONObject jsonObject = new JSONObject();
                try {
                    jsonObject.put("userId", userId);
                    jsonObject.put("curUserId",GetCurrentUser.currentUser.getUserId());
                    jsonObject.put("function",1);
                }catch (JSONException e){
                    e.printStackTrace();
                }
                UserInfoAsynTask userInfoAsynTask = new UserInfoAsynTask(jsonObject);
                userInfoAsynTask.setOnDataFinishedListener(new OnDataFinishedListener() {
                    @Override
                    public void onDataSuccessfully(Object data) {
                        JSONObject jsonObject = new JSONObject();
                        try {
                            String result = jsonObject.getString("result");

                        }catch (JSONException e){

                        }finally {
                            focus = !focus;
                            if(focus) {
                                btn_focus.setText("已关注");
                            }
                            else {
                                btn_focus.setText("关注");
                            }
                            btn_focus.setEnabled(true);
                        }
                    }
                    @Override
                    public void onDataFailed() {
                        btn_focus.setEnabled(true);
                    }
                });
                userInfoAsynTask.execute();
            }
        });
    }
    private void init(){
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("userId", userId);
            jsonObject.put("curUserId",GetCurrentUser.currentUser.getUserId());
            jsonObject.put("function",0);
        }catch (JSONException e){
            e.printStackTrace();
        }
        UserInfoAsynTask userInfoAsynTask = new UserInfoAsynTask(jsonObject);
        userInfoAsynTask.setOnDataFinishedListener(new OnDataFinishedListener() {
            @Override
            public void onDataSuccessfully(Object data) {
                JSONObject jsonObject =null;
                try {
                    jsonObject = new JSONObject(data.toString());
                    user_name = jsonObject.getString("userName");
                    user_email = jsonObject.getString("userEmail");
                    textView_name.setText(user_name);
                    textView_email.setText(user_email);
                }catch (JSONException e){
                    e.printStackTrace();
                }

                    //
                try {
                    user_face_URL = jsonObject.getString("userHeadURL");
                    AsyncImageLoader asyncImageLoader = new AsyncImageLoader();
                    String imageUrl = ConfigURL.headURL+ user_face_URL;
                    Drawable cachedImage = asyncImageLoader.loadDrawable(imageUrl, new AsyncImageLoader.ImageCallback() {
                        public void imageLoaded(Drawable imageDrawable, String imageUrl) {

                            if (imageDrawable == null) {
                                //imageView.setImageResource(R.drawable.icon);
                                imageDrawable = getResources().getDrawable(R.drawable.user_image);
                            }
                            iamgeView_face.setImageDrawable(imageDrawable);
                        }
                    });
                }catch (JSONException e){
                    iamgeView_face.setImageResource(R.drawable.user_image);
                    textView_des.setText(user_des);
                }
                try{
                    user_firstPic_URL = jsonObject.getString("userFirstPicURL");
                    AsyncImageLoader asyncImageLoader = new AsyncImageLoader();
                    String imageUrl = ConfigURL.photoURL+ user_firstPic_URL;
                    Drawable cachedImage = asyncImageLoader.loadDrawable(imageUrl, new AsyncImageLoader.ImageCallback() {
                        public void imageLoaded(Drawable imageDrawable, String imageUrl) {

                            if (imageDrawable == null) {
                                //imageView.setImageResource(R.drawable.icon);
                                imageDrawable = getResources().getDrawable(R.drawable.no_pic);
                            }
                            imageButton_pic.setImageDrawable(imageDrawable);
                        }
                    });
                }catch (JSONException e){
                    imageButton_pic.setImageResource(R.drawable.no_pic);
                    //imageButton_pic.setEnabled(false);
                }
                try {
                    user_des = jsonObject.getString("userDes");
                }catch (JSONException e){
                    textView_des.setText("这个人很懒，神马简介都没有。。。");
                }
                try {
                    user_address = jsonObject.getString("userAddress");
                    textView_address.setText(user_address);
                }catch (JSONException e){
                    textView_address.setText("");
                }
              try {
                  user_born = jsonObject.getString("userBorn");
                  textView_born.setText(user_born);
              }catch (JSONException e){
                  textView_born.setText("");
              }
              try {
                  user_sex = jsonObject.getString("userSex");
                  textView_sex.setText(user_sex);
              }catch (JSONException e){
                  textView_sex.setText("");
              }
                try {
                    focus = jsonObject.getBoolean("focus");
                    if(focus){
                        btn_focus.setText("已关注");
                    }else
                        btn_focus.setText("关注");
                }catch (JSONException e){

                }
            }

            @Override
            public void onDataFailed() {

            }
        });
        userInfoAsynTask.execute();
    }
}
